<?php
namespace Ekadmin\Controller;
use Think\Controller;
use Think\Page;
use Ekadmin\Model\AuthRuleModel;
class RuleController extends Controller {//CommonController
    //显示权限列表
    public function index(){
        $AuthRule=new AuthRuleModel();
        $where=" 1=1 ";
        $rs=$AuthRule->pageinfo("auth_rule", $where, "id desc");
        if(count($rs["data"])>0){
            for($i=0;$i<count($rs["data"]);$i++){
                if($rs["data"][$i]["pid"]>0){
                    $rt=M("auth_rule")->find($rs["data"][$i]["pid"]);
                    $rs["data"][$i]["parentname"]=$rt["title"];
                }else{
                    $rs["data"][$i]["parentname"]="-";
                }
                
            }
        }
        $this->count=$rs["count"];
        $this->info=$rs["data"];
        $this->page=$rs["page"];
        $this->display();
    }
    //显示添加规则界面
    public function add(){
        $rs=M("auth_rule")->where("status=1 and pid=0")->order("id desc")->select();
        if($rs){
            for($i=0;$i<count($rs);$i++){
                $rs[$i]["title"]="|--".$rs[$i]["title"];
                $rt=M("auth_rule")->where("status=1 and pid=".$rs[$i]["id"])->order("id desc")->select();
                for($j=0;$j<count($rt);$j++){
                    $rt[$j]["title"]="|------".$rt[$j]["title"];
                }
                $rs[$i]["sub"]=$rt;
            }
            $this->info=$rs;
        }else{
            $this->info=array();
        }
        $this->display();
    }
    
    //添加规则操作
    public function add_handle(){
        if(!IS_POST || !isset($_POST["title"]) || !isset($_POST["name"]) || !isset($_POST["type"]) || trim($_POST["type"])==""){
            ajax_return(1, "非法操作");
        }
        if(trim($_POST["title"])==""){
            ajax_return(1, "参数不能为空");
        }
        $data=array(
            "title"=>I("title"),
            "name"=>I("name"),
            "pid"=>intval($_POST["type"]),
            "status"=>intval($_POST["status"]),
            "addtime"=>date("Y-m-d H:i:s"),
            "ico"=>I("ico")
        );
        $authrule=new AuthRuleModel();
        if($authrule->aselect("auth_rule", "name='".$data["name"]."' and title='".$data["title"]."' and pid=".$data["pid"])){
            ajax_return(1, "信息已存");
        }else{
            $authrule->aadd("auth_rule", $data);
            ajax_return(0,"操作成功",U("Rule/index"));
        }
    }
    //显示修改规则界面
    public function modi(){
        if(!isset($_POST["nid"]) || intval($_POST["nid"])<=0){
            $this->error("非法操作");
        }
        $nid=intval($_POST["nid"]);
        $this->info=M("auth_rule")->where("id=".$nid)->find();
        if($rs=M("auth_rule")->field("id,title")->where("pid=0 and id!=".$nid)->order("id desc")->select()){
            for($i=0;$i<count($rs);$i++){
                $rs[$i]["title"]="|--".$rs[$i]["title"];
                $rt=M("auth_rule")->field("id,title")->where("pid=".$rs[$i]["id"]." and id!=".$nid)->order("id desc")->select();
                if($rt){
                    for($j=0;$j<count($rt);$j++){
                        $rt[$j]["title"]="|------".$rt[$j]["title"];
                    }
                    $rs[$i]["sub"]=$rt;
                }else{
                    $rs[$i]["sub"]=array();
                }
            }
            $this->type=$rs;
        }else{
            $this->type=array();
        }
        $this->display();
    }
    //提交规则修改操作
    public function modi_handle(){
        if(!IS_POST || !isset($_POST["name"]) || !isset($_POST["title"])){
            $this->error("非法操作");
        }
        $nid=intval($_POST["nid"]);
        $data=array(
            "name"=>I("name"),
            "title"=>I("title"),
            "status"=>intval($_POST["status"]),
            "pid"=>intval($_POST["type"]),
            "ico"=>I("ico")
        );
        $authrule=new AuthRuleModel();
        $where="name='".$data["name"]."' and title='".$data["title"]."' and pid=".$data["pid"]." and id!=".$nid;
        if($authrule->aselect("auth_rule", $where)){
            ajax_return(1, "记录已存在");
        }else{
            $authrule->aupdate("auth_rule", $data, "id=".$nid);
            ajax_return(0, "操作成功",U("Rule/index"));
        }
        
    }
    
    
    //全选删除操作
    public function alldel(){
        if(!$_POST || trim($_POST["allid"])=="" ){
            $this->error("非法操作");
        }
        $allid=substr(trim($_POST["allid"]),0,-1);
        if($allid==""){
            $data=array(
                "status"=>0,
                "retDesc"=>"操作失败，必须要选择要删除的记录"
            );
            $this->ajaxReturn($data,"json");
        }
        $rs=M("auth_rule")->where("pid in (".$allid.")")->select();
        if($rs){
            $data=array(
                "status"=>0,
                "retDesc"=>"删除的分类下面还有具体权限信息，请删除之后再操作。"
            );
            $this->ajaxReturn($data,"json");
        }else{
            $authrule=new AuthRuleModel();
            $authrule->del("auth_rule_class","id in (".$allid.")");
            $data=array(
                "status"=>1,
                "retDesc"=>"操作成功"
            );
            $this->ajaxReturn($data);
        }
    }
    
    
    //单个删除权限规则操作
    public function del(){
        if(!IS_POST || intval($_POST["id"])<=0){
            ajax_return(1,'非法操作');
        }
        $nid=intval($_POST["id"]);
        $model=new AuthRuleModel();
        $rs=$model->del("auth_rule", "id=".$nid);
        if($rs){
            ajax_return(0,'删除成功');
        }else{
            ajax_return(1,'删除失败');
        }
    }
    //多选删除权限规则操作
    public function rulealldel(){
        if(!$_POST || trim($_POST["allid"])=="" ){
            $this->error("非法操作");
        }
        $allid=substr(trim($_POST["allid"]),0,-1);
        if($allid==""){
            $data=array(
                "status"=>0,
                "retDesc"=>"操作失败，必须要选择要删除的记录"
            );
            $this->ajaxReturn($data,"json");
        }
        $authrule=new AuthRuleModel();
        $authrule->del("auth_rule", "id in (".$allid.")");
        $data=array(
            "status"=>1,
            "retDesc"=>"操作成功"
        );
        $this->ajaxReturn($data,"json");
    }
    
    
    //显示角色列表界面
        public function rule_index(){
        $authrule=new AuthRuleModel();
        $rs=$authrule->pageinfo("auth_group", "1=1", "id desc");
        $this->count=$rs["count"];
        $arr=$rs["data"];
        for($i=0;$i<count($arr);$i++){
            $rs["data"][$i]["subname"]="";
            $res=M("auth_rule")->where("id in (".$arr[$i]["rules"].")")->select();
            for($j=0;$j<count($res);$j++){
                if($rs["data"][$i]["subname"]!=""){
                    $rs["data"][$i]["subname"].=",".$res[$j]["title"];
                }else{
                    $rs["data"][$i]["subname"]=$res[$j]["title"];
                }
            }
        }
        $this->info=$rs["data"];
        $this->page=$rs["page"];
        $this->display();
    }
    
    //角色全选删除操作
    public function rule_alldel(){
        if(!$_POST || trim($_POST["allid"])=="" ){
            $this->error("非法操作");
        }
        $allid=substr(trim($_POST["allid"]),0,-1);
        if($allid==""){
            $data=array(
                "status"=>0,
                "retDesc"=>"操作失败，必须要选择要删除的记录"
            );
            $this->ajaxReturn($data,"json");
        }

        $authrule=new AuthRuleModel();
        $authrule->del("auth_group", "id in (".$allid.")");
        $data=array(
            "status"=>1,
            "retDesc"=>"操作成功"
        );
        $this->ajaxReturn($data,"json");
        
    }
    
    //显示修改角色界面
    public function role_modi(){
        if(!isset($_POST["nid"]) || intval($_POST["nid"])<=0){
            $this->error("非法操作");
        }
        $nid=intval($_POST["nid"]);
        $authrule=new AuthRuleModel();
        $rs=$authrule->afind("auth_group", "id=".$nid);
        if(!$rs){
            $this->error("记录不存在");
        }
        $this->rs=$rs;
        $rs=$authrule->aselect("auth_rule", " pid=0 ");
        if($rs){
            for($i=0;$i<count($rs);$i++){
                if($rt=$authrule->aselect("auth_rule", " pid= ".$rs[$i]["id"])){
                    for($j=0;$j<count($rt);$j++){
                        $rt[$j]["sub"]=$authrule->aselect("auth_rule", " pid= ".$rt[$j]["id"]);
                    }
                    $rs[$i]["sub"]=$rt;
                }
            }
        }
        $this->info=$rs;
        $this->display();
    }
    //提交修改角色信息
    public function role_modi_handle(){
        if(!IS_POST || !isset($_POST["title"]) || trim($_POST["title"])=="" || intval($_POST["id"])<=0){
            ajax_return(1,"非法操作");
        }
        $data=array(
            "title"=>I("title"),
            "status"=>intval($_POST["status"]),
            "rules"=>implode(",",$_POST["nid"])
        );
        $authrule=new AuthRuleModel();
        if($authrule->afind("auth_group", " title='".$data["title"]."' and id!=".intval($_POST["id"]))){
            ajax_return(1,"角色名称已存在");
        }else{
            $authrule->aupdate("auth_group", $data, "id=".intval($_POST["id"]));
            ajax_return(0,"操作成功",U("Rule/rule_index"));
        }
    }
    
    
    //显示添加角色界面
    public function rule_add(){
        $authrule=new AuthRuleModel();
        $rs=$authrule->aselect("auth_rule", "status=1 and pid=0");
        if($rs){
            for($i=0;$i<count($rs);$i++){
                $rs[$i]["sub"]=array();
                if($rt=$authrule->aselect("auth_rule", "pid='".$rs[$i]["id"]."' and status=1")){
                    for($j=0;$j<count($rt);$j++){
                        $res=$authrule->aselect("auth_rule", "pid='".$rt[$j]["id"]."' and status=1");
                        $rt[$j]["sub"]=$res;
                    }
                    $rs[$i]["sub"]=$rt;
                }
            }
        }
        $this->info=$rs;
        $this->display();
    }
    
    //提交角色信息操作
    public function rule_add_handle(){
        if(!IS_POST || trim($_POST["title"])=="" || count($_POST["nid"])<=0 ){
            ajax_return(0, "非法操作");
        }
        $data=array(
            "title"=>I("title"),
            "status"=>intval($_POST["status"]),
            "rules"=>implode(",",$_POST["nid"]),
            "addtime"=>date("Y-m-d H:i:s")
        );
        
        if(M("auth_group")->where("title='".$data["title"]."'")->find()){
            ajax_return(1, "记录重复");
        }else{
            $authrule=new AuthRuleModel();
            $authrule->aadd("auth_group", $data);
            ajax_return(0,"添加成功",U("Rule/rule_index"));
        }
        
    }
    //单个删除角色操作
    public function role_del(){
        if(!isset($_POST["id"]) || intval($_POST["id"])<=0){
            ajax_return(1, "非法操作");
        }
        $nid=intval($_POST["id"]);
        
        M("auth_group")->where("id=".$nid)->delete();
        ajax_return(0, "删除成功",U("Rule/rule_index"));
    }
}